package com.jt.tenant.conn;

import com.jt.tenant.app.JTApplication;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by AD on 2016/5/17.
 */
@HttpInlet(Conn.INLET_GET_MAINTAIN_REMIND)
public class GetMaintainRemind extends JTAsyGet<GetMaintainRemind.Info> {
    public String token = JTApplication.jtPreferences.readToken();
    public String userid = JTApplication.jtPreferences.readUserID();

    public GetMaintainRemind(AsyCallBack<Info> asyCallBack) {
        super(asyCallBack);
    }

    @Override
    protected Info parser(JSONObject object) {
        JSONObject apiTYF = object.optJSONObject("apiTYF");

        JSONObject head = apiTYF.optJSONObject("head");

        Info info = new Info();

        info.msg = head.optString("msg");

        info.code = head.optString("code");

        JSONObject body = apiTYF.optJSONObject("body");

        JSONArray array = body.optJSONArray("list");

        if(array.length()==1){
            try {
                JSONObject obj = array.getJSONObject(0);
                info.NEXT_MAINTENANCE_DISTANCE = obj.optString("NEXT_MAINTENANCE_DISTANCE");
                info.NEXT_MAINTENANCE_TIME = obj.optString("NEXT_MAINTENANCE_TIME");
                info.TOTAL_MAINTENANCE_DISTANCE = obj.optString("TOTAL_MAINTENANCE_DISTANCE");
                info.LAST_MAINTENANCE_DISTANCE = obj.optString("LAST_MAINTENANCE_DISTANCE");
                info.LAST_MAINTENANCE_TIME = obj.optString("LAST_MAINTENANCE_TIME");
                info.meilage_space = obj.optString("MAINTENANCE_DISTANCE_SPACE");
                info.time_space = obj.optString("MAINTENANCE_TIME_SPACE");
                info.ACTIVE = obj.optString("ACTIVE");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if(array.length()==2){
            try {
                JSONObject obj1 = array.getJSONObject(0);
                info.TOTAL_MAINTENANCE_DISTANCE = obj1.optString("TOTAL_MAINTENANCE_DISTANCE");
                info.LAST_MAINTENANCE_DISTANCE = obj1.optString("LAST_MAINTENANCE_DISTANCE");
                info.ACTIVE = obj1.optString("ACTIVE");
                info.LAST_MAINTENANCE_TIME = obj1.optString("LAST_MAINTENANCE_TIME");
                JSONObject obj2 = array.getJSONObject(1);
                info.NEXT_MAINTENANCE_DISTANCE = obj2.optString("NEXT_MAINTENANCE_DISTANCE");
                info.NEXT_MAINTENANCE_TIME = obj2.optString("NEXT_MAINTENANCE_TIME");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return info;
    }

    public static class Info{
        public String msg;
        public String code;
        public String NEXT_MAINTENANCE_DISTANCE;
        public String NEXT_MAINTENANCE_TIME;
        public String TOTAL_MAINTENANCE_DISTANCE;
        public String LAST_MAINTENANCE_DISTANCE;
        public String LAST_MAINTENANCE_TIME;
        public String time_space;
        public String meilage_space;
        public String ACTIVE;
    }

}
